<?php

namespace app\apiadmin\service\redis;

use Predis\Client;

class MsgService
{
    private $redis;

    public function __construct()
    {
        //$tcpUrl = 'tcp://'.env('cache.host').':'.env('cache.port');
        $tcpUrl = 'tcp://172.28.179.129:6379';
        $tcpArray = [
            'scheme' => 'tcp',
            'host' => env('redis.host'),
            'port' => env('redis.port'),
            "password" => env('redis.password'),
            "username" => env('redis.username'),
            //'password' => env('cache.password'),
        ];
        $this->redis = new Client($tcpArray);
    }

    //发送消息
    public static function send($channel, $message)
    {
        $msg = new static();
        return $msg->sendDynamic($channel, $message);
    }

    //非静态-发送消息
    public function sendDynamic($channel, $message)
    {
        return $this->redis->publish($channel, $message);
    }

    //发送添加
    public static function sendAdd($liveId, $msg)
    {
        $params = [
            'command' => 'addword',
            'params' => [
                'LiveId' => (string) $liveId,
                "WordList" => [$msg],
            ],
        ];
        $json = json_encode($params);
        return self::send(env("redis.prefix") . 'live_sensitive_word_event', $json);
    }

    //发送删除
    public static function sendDel($liveId, $msg)
    {
        $params = [
            'command' => 'delword',
            'params' => [
                'LiveId' => (string) $liveId,
                "WordList" => [$msg],
            ],
        ];
        $json = json_encode($params);
        return self::send(env("redis.prefix") . 'live_sensitive_word_event', $json);
    }

    //发送加载直播严禁词库
    public static function sendStartLiveMessage($liveId)
    {
        //创建成功，通知词库更新
        $params = [
            'command' => 'addlive',
            'params' => [
                'LiveId' => (string) $liveId,
            ],
        ];
        $json = json_encode($params);
        return self::send(env("redis.prefix") . 'live_sensitive_word_event', $json);
    }

    //发送卸载直播违禁词库
    public static function sendEndLiveMessage($liveId)
    {
        //创建成功，通知词库更新
        $params = [
            'command' => 'dellive',
            'params' => [
                'LiveId' => (string) $liveId,
            ],
        ];
        $json = json_encode($params);
        return self::send(env("redis.prefix") . 'live_sensitive_word_event', $json);
    }

    //发送重载直播严禁词库
    public static function sendRedisReloadLiveMessage($liveId)
    {
        //创建成功，通知词库更新
        $params = [
            'command' => 'reloadLive',
            'params' => [
                'LiveId' => (string) $liveId,
            ],
        ];
        $json = json_encode($params);
        return self::send(env("redis.prefix") . 'live_sensitive_word_event', $json);
    }

}
